﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using WasaWasa.Converters;

namespace WasaWasa.Controls
{
    /// <summary>
    /// NumericUpDown.xaml の相互作用ロジック
    /// </summary>
    //TODO:MinValueとMaxValueとStepValueを依存プロパティ化する
    public partial class NumericUpDown : UserControl, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged メンバ
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private static ValidationRule integerRule = new DecimalInInt32ValidationRule();

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(0M, new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            NumericUpDown nud = p as NumericUpDown;
            decimal value = (decimal)e.NewValue;

            if (nud.PropertyChanged != null)
                nud.PropertyChanged(nud, new PropertyChangedEventArgs("Value"));
        }), new CoerceValueCallback(delegate(DependencyObject p, object o)
        {
            NumericUpDown nud = p as NumericUpDown;
            decimal value = (decimal)o;

            if (value < nud.MinValue)
                value = nud.MinValue;
            if (nud.MaxValue < value)
                value = nud.MaxValue;

            return value;
        })));

        public static readonly DependencyProperty IsIntegerOnlyProperty =
            DependencyProperty.Register("IsIntegerOnly", typeof(bool), typeof(NumericUpDown),
            new PropertyMetadata(new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            NumericUpDown nud = p as NumericUpDown;
            bool value = (bool)e.NewValue;
            bool oldValue = (bool)e.OldValue;

            if (value)
            {
                if (oldValue)
                    return;
                nud.AddValidationRule(NumericUpDown.integerRule);
            }
            else
            {
                if (!oldValue)
                    return;
                nud.RemoveValidationRule(NumericUpDown.integerRule);
            }
        })));

        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(decimal.MinValue, new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
        {
            NumericUpDown nud = p as NumericUpDown;
            decimal value = (decimal)e.NewValue;

            (nud.FindResource("decimalRangeValidationRule") as DecimalRangeValidationRule).MinValue = value;
            if (nud.Value < value)
                nud.Value = value;
        })));

        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(decimal.MaxValue, new PropertyChangedCallback(delegate(DependencyObject p, DependencyPropertyChangedEventArgs e)
            {
                NumericUpDown nud = p as NumericUpDown;
                decimal value = (decimal)e.NewValue;

                (nud.FindResource("decimalRangeValidationRule") as DecimalRangeValidationRule).MaxValue = value;
                if (value < nud.Value)
                    nud.Value = value;
        })));

        public static readonly DependencyProperty StepValueProperty =
            DependencyProperty.Register("StepValue", typeof(decimal), typeof(NumericUpDown),
            new PropertyMetadata(1M), new ValidateValueCallback(delegate(object o)
        {
            return (decimal)o > 0;
        }));

        public bool IsIntegerOnly
        {
            get { return (bool)GetValue(IsIntegerOnlyProperty); }
            set
            {
                SetValue(IsIntegerOnlyProperty, value);

                PropertyChanged(this, new PropertyChangedEventArgs(ValueProperty.Name));
            }
        }

        public decimal Value
        {
            get { return (decimal)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public decimal MinValue
        {
            get { return (decimal)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }

        public decimal MaxValue
        {
            get { return (decimal)GetValue(MaxValueProperty); }
            set
            {
                SetValue(MinValueProperty, value);
            }
        }

        public decimal StepValue
        {
            get { return (decimal)GetValue(StepValueProperty); }
            set
            {
                SetValue(StepValueProperty, value);
            }
        }

        public NumericUpDown()
        {
            InitializeComponent();
            txtValue.DataContext = this;
            btnUp.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
            {
                Value += StepValue;
            });
            btnDown.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs e)
            {
                Value -= StepValue;
            });
        }

        public void AddValidationRule(ValidationRule rule)
        {
            binding.ValidationRules.Add(rule);
        }

        public void RemoveValidationRule(ValidationRule rule)
        {
            binding.ValidationRules.Remove(rule);
        }
    }
}
